<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Form jQuery Validation</title>
    <link rel="shortcut icon" type="image/x-icon" href="Vertical/favicon.ico">
    <!-- google font -->
    <link rel="shortcut icon" href="favicon.ico">
    <link href="${pageContext.request.contextPath}/static/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/css/font-awesome.min.css?v=4.4.0" rel="stylesheet">
<%--    <link href="${pageContext.request.contextPath}/staitc/css/animate.css" rel="stylesheet">--%>
    <link href="${pageContext.request.contextPath}/static/css/style.css?v=4.1.0" rel="stylesheet">

    <!--form semantic style-->
<%--    <link href="${pageContext.request.contextPath}/static/style.css" rel="stylesheet">--%>
<%--    <link href="${pageContext.request.contextPath}/static/" rel="stylesheet">--%>
</head>
<body>
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="ibox-title">
            <h5>缺勤信息</h5>
        </div>
    </div>
    <div class="row">
        <div style="float: left;margin-left: 6%">
            <button class="btn btn-primary"  onclick="addstudent()">缺勤登记</button>
            <a onclick=" " href="" class="btn btn-primary "><i class="fa fa-refresh"></i></a>
        </div>
        <div style="margin-left: 80%">
            <input  type="text" style="width: 200px;height: 30px;" id="selectStudentName" placeholder="按学生名字查询，输入关键字即可">
            <button class="btn btn-primary"  onclick="selectStudentByName()">搜索</button>
        </div>
    </div>
    <div class="margin">
        <table class="table table-striped table-bordered table-hover " id="" style="text-align: center;width: 90%;margin-left: 5%;margin-right: 5%">
            <thead>
            <tr>
                <td>#</td>
                <td>学生学号</td>
                <td>学生姓名</td>
                <td>缺勤课程</td>
                <td>缺勤时间</td>
                <td>操作</td>
            </tr>
            </thead>
            <tbody id="tb">
            </tbody>

        </table>
    </div>
    <!-- 分页区域-->
    <div class="margin" style="margin-left: 5%">
        每页显示<select id="pageSizeSelect"  onchange="onSelectChange()">
        <option value="5" selected="selected">5</option>
        <option value="10">10</option>
        <option value="15">15</option>
    </select>条数据，当前是第<span id="currentPageSpan"></span>页，
        总共<span id="totalPageSpan"></span> 页
        <button type="button" class="btn btn-success" onclick="firstPage()">首页</button>
        <button type="button" class="btn btn-success" onclick="prePage()">上一页</button>
        <button type="button" class="btn btn-success" onclick="nextPage()">下一页</button>
        <button type="button" class="btn btn-success" onclick="lastPage()">尾页</button>
    </div>
    <%--这里的div做一个编辑用户信息的模态框--%>
    <div class="modal" id="editModal">
        <%--对话框--%>
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h4>缺勤登记</h4>
                </div>
                <div class="modal-body">
                    <label class="control-label">学生姓名</label>
                    <input class="form-control" id="studentName" placeholder="请输入学生姓名">
                    <label class="control-label">缺勤课程</label>
                    <input class="form-control" id="courseName" placeholder="请输入缺勤课程">
                    <label class="control-label">缺勤时间</label>
                    <input class="form-control" id="absenceTime" placeholder="请输入缺勤时间">
                </div>
                <div class="modal-footer">
                    <button class="button" data-dismiss="modal">取消</button>
                    <button class="button" onclick="addok()">确定</button>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- 全局js -->
<script src="${pageContext.request.contextPath}/static/js/jquery.min.js?v=2.1.4"></script>
<script src="${pageContext.request.contextPath}/static/js/bootstrap.min.js?v=3.3.6"></script>
<script src="${pageContext.request.contextPath}/static/js/plugins/metisMenu/jquery.metisMenu.js"></script>
<script src="${pageContext.request.contextPath}/static/js/plugins/slimscroll/jquery.slimscroll.min.js"></script>
<script src="${pageContext.request.contextPath}/static/js/plugins/layer/layer.min.js"></script>

<!-- 自定义js -->
<script src="${pageContext.request.contextPath}/static/js/hplus.js?v=4.1.0"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/js/contabs.js"></script>
<!-- 第三方插件 -->
<script src="${pageContext.request.contextPath}/static/js/plugins/pace/pace.min.js"></script>

<script>
    //当前页码
    var currentPage=1;
    //每页的记录个数
    var pageSize=5;
    //总共多少页
    var  totalPage;
    $(function(){
        //显示当前分页数据
         loadListByPage();

    });
    //用户修改的每页的记录数
    function onSelectChange() {
        pageSize = $("#pageSizeSelect").val();
        loadListByPage();
    }
    //首页的回调方法
    function firstPage() {
        currentPage = 1;
        loadListByPage();
    }
    //上一页回调方法
    function prePage() {
        currentPage--;
        if(currentPage < 1){
            currentPage = 1;
        }
        loadListByPage();
    }
    //下一页回调方法
    function nextPage() {
        currentPage++;
        if(currentPage > totalPage){
            currentPage = totalPage;
        }
        loadListByPage();
    }
    //尾页回调方法
    function lastPage() {
        currentPage = totalPage;
        loadListByPage();
    }

    function showList(absenlist) {
        var html = '';
        //遍历userList数组
        for (var i = 0; i < absenlist.length; i++) {
            //拿到一个用户信息
            var item = absenlist[i];
            html += "<tr>";
            html += "<td>" + item.absenceId + "</td>"; //显示缺勤的ID
            html += "<td>" + item.studentId + "</td>"; //显示缺勤学生的学号
            html += "<td>" + item.studentName + "</td>";//显示缺勤学生的名字
            html += "<td>" + item.courseName + "</td>";//显示缺勤学生的课程ID
            html += "<td>" + item.absenceTime + "</td>";//显示缺勤学生的课程
            html += "<td><button type=\"button\" class=\"btn btn-success\" onclick='deleteOne(\"" + item.absenceId + "\")'>删除</button>"
            html += "</tr>";
        }
        $("#tb").html(html);
    }
    //分页显示用户信息
    function loadListByPage() {
        $.ajax({
            url:"${pageContext.request.contextPath}/absenceController/absenceByPage",
            type: "get",
            data:{"currentPage":currentPage,"pageSize":pageSize},
            dataType: 'json',
            success:function (result) {
              if (result.code==0){
                  showList(result.absencelist);
                  totalPage=result.totalPage;
                  $("#totalPageSpan").html(totalPage);
                  $("#currentPageSpan").html(currentPage);
              }
            },
            error:function (err) {
                console.log(JSON.stringify(err));
            }
        })
    }
    //打开模态框
    function addstudent() {
        $("#editModal").modal("show");
    }
    //根据模态框中输入的内容来进行插入数据库操作
    function addok() {
            var studentName=$("#studentName").val();
            var courseName=$("#courseName").val();
            var absenceTime=$("#absenceTime").val();
            $.ajax({
                url:"${pageContext.request.contextPath}/absenceController/addOne",
                type:"post",
                data: {
                    "studentName":studentName,
                    "courseName":courseName,
                    "absenceTime":absenceTime,
                },
                contentType: "application/x-www-form-urlencoded",//指明一个简单的表单提交
               dataType: "json",
                success:function (result) {
                      if (result.code==0){
                          //关闭模态框
                          $("#editModal").modal("hide");
                          //显示数据
                          loadListByPage();
                          alert(result.message);
                      }else{
                               alert(result.message);
                      }
                },
                error:function () {
                    console.log("添加ajax请求失败")
                }
            })
    }
    //删除缺课学生信息
    function deleteOne(absenceId) {
        $.ajax({
            url:"${pageContext.request.contextPath}/absenceController/deleted",
            type:"get",
            contentType: "application/x-www-form-urlencoded",
            data:{"absenceId":absenceId},
            dataType:'json',
            success:function (result) {
              if (result.code==0){
                    loadListByPage();
                    alert(result.message);
              }
            },
            error:function (err) {
                  alert("ajax请求失败")
            }
        })
    }
    //根据姓名查询学生
    function selectStudentByName() {
           var studentName=$("#selectStudentName").val();
           $.ajax({
               url:"${pageContext.request.contextPath}/absenceController/seletedByName",
               type:"get",
               data:{"studentName":studentName},
               dataType:'json',
               contentType:"application/x-www-form-urlencoded",
               success:function (result) {
                   if(result.code== 0){
                       showList(result.absence);
                   }else {
                       alert(result.message);
                   }
               },
               error:function (err) {
                   alert("模糊查询失败ajax请求失败");
               }

           })
    }
</script>
</body>
</html>
